﻿using System.Collections.Generic;
using System.Linq;
using CleverDD.Model.Communication.PLC;
using HslCommunication;

namespace CleverDD.Helper.Communication.PLC
{
    public class PlcTool
    {
        private BasePlcReadAndWrite _basePlc;//操作Plc的对象
        private PlcInfoEntity _plcInfo;//Plc信息
        public List<PlcAddressInfoEntity> CurrentPlcAddressInfo;//当前这个Plc对应的地址信息
        public PlcTool(PlcInfoEntity plcInfo) 
        {
            _plcInfo = plcInfo;
            CurrentPlcAddressInfo = StaticTool.PlcAddressInfoList.Where(item => item.PlcInfoGuid == _plcInfo.Guid).ToList();
            switch (plcInfo.PlcType)
            {
                case PlcType.基恩士Binary:
                    _basePlc = new KeyenceBasePlcBinary();
                    break;
                case PlcType.三菱MelsecMcNet:
                    _basePlc = new MelsecMcNetBasePlc();
                    break;
                case PlcType.西门子300:
                case PlcType.西门子400:
                case PlcType.西门子1200:
                case PlcType.西门子1500:
                case PlcType.西门子200Smart:
                    _basePlc = new SiemensBasePlc(plcInfo.PlcType);
                    break;

                default:
                    _basePlc = new BasePlcReadAndWrite();
                    break;
            }
        }

        public OperateResult ConnPlc()
        {
            OperateResult operateResult = new OperateResult();
            if (!_plcInfo.IsInUse)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = "PLC未启用";
                return operateResult;
            }
            if (_basePlc.IsConnected) 
            {
                operateResult.IsSuccess = false;
                operateResult.Message = "PLC已连接";
                return operateResult;
            }

            operateResult = _basePlc.ConnPlc(_plcInfo.Ip, _plcInfo.Port);
            _basePlc.IsConnected = operateResult.IsSuccess;
            return operateResult;
        }

        public OperateResult ClosePlc()
        {
            if (!_plcInfo.IsInUse)
            {
                OperateResult operateResult = new OperateResult();
                operateResult.IsSuccess = false;
                operateResult.Message = "PLC未启用";
                return operateResult;
            }

            return _basePlc.ClosePlc();
        }


        public OperateResult ReadAll()
        {
            if (!_basePlc.IsConnected)
            {
                OperateResult operateResult = new OperateResult();
                operateResult.IsSuccess = false;
                operateResult.Message = "PLC未连接";
                return operateResult;
            }

            return _basePlc.ReadAll(CurrentPlcAddressInfo);
        }

        public OperateResult WritePlcValue(int enumIndex, string value)
        {

            OperateResult operateResult = new OperateResult();
            if (!_basePlc.IsConnected)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = "PLC未连接";
                return operateResult;
            }

            if (enumIndex >= CurrentPlcAddressInfo.Count)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = "数组越界";
                return operateResult;
            }

            if (CurrentPlcAddressInfo[enumIndex].State == PlcState.停用)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = "PLC停用";
                return operateResult;
            }

            return _basePlc.WritePlc(CurrentPlcAddressInfo[enumIndex], value);


        }


    }
}
